﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_SwitchField:PageForm_SingleInputFieldBase
    {
        private string _value = "";
        private bool _disabled;
        private string _onStateText="ON";
        private string _offStateText="OFF";
        private bool _isOn=false;
        private string _onChangedScript="";

        public PageForm_SwitchField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_switchfield");
        }

        /// <summary>
        /// 当状态为on时,字段的值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageForm_SwitchField Value(string value)
        {
            _value=value;
            return this;
        }
        
        /// <summary>
        /// 是否为选中状态
        /// </summary>
        /// <param name="isOn"></param>
        /// <returns></returns>
        public PageForm_SwitchField State(bool isOn){
            _isOn=isOn;

            return this;
        }

        /// <summary>
        /// 是否为选中状态
        /// </summary>
        /// <param name="isOn"></param>
        /// <returns></returns>
        public PageForm_SwitchField State(bool? isOn){
            _isOn=isOn==true;

            return this;
        }

        /// <summary>
        /// on状态和off状态下的标签文本
        /// </summary>
        /// <param name="onStateLabel"></param>
        /// <param name="offStateLabel"></param>
        /// <returns></returns>
        public PageForm_SwitchField StateLabel(string onStateLabel,string offStateLabel){
            _onStateText=onStateLabel;
            _offStateText=offStateLabel;

            return this;
        }

        public PageForm_SwitchField OnChangedScript(string script){
            _onChangedScript=script;
            return this;
        }

        public PageForm_SwitchField Disabled(bool disabled=true){
            _disabled=disabled;

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            //<input type="checkbox" name="yyy" lay-skin="switch" lay-text="ON|OFF" checked>

            writer.WriteLine($"<input type=\"checkbox\" name=\"{_name}\" onchange='{_onChangedScript}' {(_disabled?"disabled":"")}  lay-skin=\"switch\" lay-text=\"{_onStateText}|{_offStateText}\"  value=\"{_value}\"  {(_isOn?"checked":"")}/>");
        }
    }
}
